package com.longan.web.controller.common;

import com.longan.common.config.RuoYiConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;

@RestController
public class ImageController {


    @GetMapping("/profile/upload/**")
    public void download(HttpServletRequest request, HttpServletResponse response) {
        try {
            String requestUri = request.getRequestURI();
            String basePath = "/profile/upload";
            if (requestUri.startsWith(basePath)) {
                String totalPath = RuoYiConfig.getUploadPath() + requestUri.substring(basePath.length());
                FileInputStream fileInputStream = new FileInputStream(totalPath);
                ServletOutputStream outputStream = response.getOutputStream();
                response.setContentType("image/jpeg");
                int len = 0;
                byte[] bytes = new byte[1024];
                while ((len = fileInputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, len);
                    outputStream.flush();
                }
                //关闭资源
                outputStream.close();
                fileInputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
